import DataManager from "../manager/DataManager";
import { privacyChannel } from "./privacyChannel";
import { privacyChannelVivo } from "./privacyChannelVivo";
import { prv } from "./prv";

declare global {
    interface Window {
        __toMain?: () => void;
    }
}
const { ccclass, property } = cc._decorator;
@ccclass
export default class privacyWin extends cc.Component {


    @property(cc.Node)
    txt_main: cc.Node = null;
    @property(cc.Node)
    bt_agree: cc.Node = null;
    @property(cc.Node)
    bt_reject: cc.Node = null;
    @property(cc.Node)
    win2: cc.Node = null;

    @property(cc.Label)
    txt: cc.Label = null;
    @property(cc.Node)
    bt_next: cc.Node = null;
    @property(cc.Node)
    bt_pre: cc.Node = null;
    @property(cc.Node)
    close: cc.Node = null;
    @property(cc.Node)
    bt_type1: cc.Node = null;
    @property(cc.Node)
    bt_type2: cc.Node = null;


    /** 隐私政策还是用户协议,0为前者，1为后者 */
    pType: number = 0;

    /** 同意后是否走toMain */
    isToMain: boolean = true;

    onLoad() {

        this.close.on(cc.Node.EventType.TOUCH_END, (e: cc.Event.EventTouch) => {
            this.closeDetail();
        });

        this.bt_pre.on(cc.Node.EventType.TOUCH_END, (e: cc.Event.EventTouch) => {
            this.txt.string = privacyChannel.privacyPrePage(this.pType);
        });

        this.bt_next.on(cc.Node.EventType.TOUCH_END, (e: cc.Event.EventTouch) => {
            this.txt.string = privacyChannel.privacyNextPage(this.pType);
        });

        this.bt_type1.on(cc.Node.EventType.TOUCH_END, (e: cc.Event.EventTouch) => {
            this.pType = 0;
            this.txt.string = privacyChannel.privacyPage1(this.pType);
        });
        this.bt_type2.on(cc.Node.EventType.TOUCH_END, (e: cc.Event.EventTouch) => {
            this.pType = 1;
            this.txt.string = privacyChannel.privacyPage1(this.pType);
        });


        this.txt.string = privacyChannel.privacyPage1(this.pType);

        this.bt_agree.on(cc.Node.EventType.TOUCH_END, (e: cc.Event.EventTouch) => {
            this.agree();
        });

        this.bt_reject.on(cc.Node.EventType.TOUCH_END, (e: cc.Event.EventTouch) => {
            this.node.active = false;
            privacyChannel.privacyReject();
            privacyChannel.exit();
        });

        this.txt_main.on(cc.Node.EventType.TOUCH_END, (e: cc.Event.EventTouch) => {
            this.win2.active = true;
        });
        this.win2.active = false;

        this.txt_main.getComponent(cc.RichText).string = '欢迎您进入《' + privacyChannel.getChannel().gameName + '》，我们非常重视您的个人信息和隐私保护。为更好地保障您的个人权益，请您仔细阅读我们的<color=#009EFF>《用户服务协议》</c>和<color=#009EFF>《隐私政策》</c>。通过这些条款您可以了解我们如何收集、使用、保护和管理这些信息。点击“同意”即表示您已阅读完毕并同意上述协议和政策中的全部内容。';
    }

    init(isToMain: boolean) {
        this.isToMain = isToMain;
    }

    closeDetail() {
        this.win2.active = false;
    }

    agree() {
        switch (privacyChannel.channel) {
            case "oppo":
            case "oppo1":
            case "xm":
            case "vivo":
            case "vivo1":
                if (privacyChannel.getChannel().getCtrl("p5") && cc.sys.localStorage.getItem("privacy") != 1) {
                    privacyChannel.getChannel().showVideo(() => {
                        // DataManager.instance.power += 1
                        // DataManager.instance.save()
                    });
                    try {
                        let style = {
                            top: prv.getSysInfo().windowHeight / 2 + 270
                        };
                        (privacyChannel.getChannel() as privacyChannelVivo).showUnqiueInter(false, style)
                    } catch (e) {
                        console.error("pricayWin", e)
                    }


                }
                break
        }
        privacyChannel.privacyAgree();
        this.node.active = false;
        if (this.isToMain) {
            console.log('=====>toMain');
            //@ts-ignore
            window.__toMain && window.__toMain();
            this.node.parent.emit("agree")
        }
    }


}
